﻿#define T_Common
using UnityEngine;
using System.Collections;


public class NScrollViewPage : MonoBehaviour
{
    public float m_PageWidth;
    public int m_EffortsFlip = 50;
    public int m_PageNums = 0;


    private int m_PageIndex = 1;                             // 当前所在页 
    private UIScrollView m_ScrollView = null;
    private float m_NowLocation;
    private bool m_IsDrag;
    private bool m_IsSpringMove;
    private SpringPanel m_Sp;
    private bool m_IsHorizontal = true;

#if T_Common
    #region 调试数据
    private float nowDistance = 0;
    #endregion

#endif

    #region 属性
    public int PageIndex
    {
        get
        {
            return m_PageIndex;
        }
    }

    #endregion

    private void Awake()
    {
        m_ScrollView = gameObject.GetComponent<UIScrollView>();
        m_Sp = gameObject.AddComponent<SpringPanel>();

        m_ScrollView.onDragStarted = OnDragStarted;
        m_ScrollView.onMomentumMove = OnMomentumMove;
        m_ScrollView.onStoppedMoving = OnStoppedMoving;

        if (m_ScrollView.movement == UIScrollView.Movement.Horizontal)
            m_IsHorizontal = true;
        else
            m_IsHorizontal = false;


        OnStoppedMoving();
    }

    private void OnDragStarted()
    {
        m_IsDrag = false;
        SetNowLocation();
    }

    private void OnMomentumMove()
    {
        if (m_IsDrag) return;

        Vector3 v3 = transform.localPosition;
        float value = 0;
        if (m_IsHorizontal)
        {
            value = m_NowLocation - v3.x;
            nowDistance = value;

            if (Mathf.Abs(value) < m_EffortsFlip) 
                return;

            if (value > 0)
            {
                if (m_PageIndex < m_PageNums) 
                    Page(-m_PageWidth);
            }
            else
            {
                if (m_PageIndex > 1) 
                    Page(m_PageWidth);
            }
        }
        else
        {
            value = m_NowLocation - v3.y;

            if (Mathf.Abs(value) < m_EffortsFlip) 
                return;

            if (value > 0)
            {
                if (m_PageIndex > 1) 
                    Page(-m_PageWidth);
            }
            else
            {
                if (m_PageIndex < m_PageNums) 
                    Page(m_PageWidth);
            }
        }
    }

    private void SpringPanleMoveEnd()
    {
        m_IsSpringMove = false;
    }

    private void OnStoppedMoving()
    {
        m_IsDrag = false;
        SetNowLocation();
    }

    private void Page(float value)
    {
        m_IsSpringMove = true;
        m_IsDrag = true;

        m_Sp.enabled = false;
        Vector3 pos = m_Sp.target;
        pos = m_IsHorizontal ? new Vector3(pos.x + value, pos.y, pos.z) : new Vector3(pos.x, pos.y + value, pos.z);


        if (!SetIndexPage(pos)) 
            return;
        
        SpringPanel.Begin(gameObject, pos, 13f).strength = 8f;
        m_Sp.onFinished = SpringPanleMoveEnd;
        
        //Debug.Log("page index=" + mPageIndex);
    }

    private void SetNowLocation()
    {
        if (m_IsHorizontal)
        {
            m_NowLocation = gameObject.transform.localPosition.x;
        }
        else
        {
            m_NowLocation = gameObject.transform.localPosition.y;
        }
    }

    private bool SetIndexPage(Vector3 v3)
    {
        float value = m_IsHorizontal ? v3.x : v3.y;
        if (m_IsHorizontal)
        {
            if (value > 0 || value < (m_PageNums - 1) * -m_PageWidth) 
                return false;
        }
        else
        {
            if (value < 0 || value > (m_PageNums - 1) * m_PageWidth) 
                return false;
        }

        value = Mathf.Abs(value);
        m_PageIndex = (int)(value / m_PageWidth) + 1;

        return true;
    }

#if T_Common
    public void OnGUI() 
    {
        GUILayout.Label("原始位置: " + m_NowLocation);
        GUILayout.Label("现在位置: " + transform.localPosition);
        GUILayout.Label("移动的距离(m_EffortsFlip): " + m_EffortsFlip);
        GUILayout.Label("啦的距离(nowDistance): " + nowDistance);
        GUILayout.Label("总距离: " + (m_PageNums - 1) * -m_PageWidth);
    }
#endif

    #region 方法
    public void PreviousPage()
    {
        if (m_IsHorizontal)
        {
            if (m_PageIndex > 1) Page(m_PageWidth);
        }
        else
        {
            if (m_PageIndex < m_PageNums) Page(m_PageWidth);
        }
    }
    
    public void NextPage()
    {
        if (m_IsHorizontal)
        {
            if (m_PageIndex < m_PageNums) Page(-m_PageWidth);
        }
        else
        {
            if (m_PageIndex > 1) Page(-m_PageWidth);
        }
    }
    #endregion
}